using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

// dllimport
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace myBase
{
    class BaseForm : Form
    {
        protected string curFileName;
        protected Bitmap curBitmap;

        public BaseForm()
        {
            Text = "图像查看器";
            Size = new Size(800, 600);

            var openBtn = new Button();
            openBtn.Parent = this;
            openBtn.Name = "open";
            openBtn.Text = "打开图像";
            openBtn.Location = new Point(37, 46);
            openBtn.Click += new EventHandler(open_Click);

            var saveBtn = new Button();
            saveBtn.Parent = this;
            saveBtn.Name = "save";
            saveBtn.Text = "保存图像";
            saveBtn.Location = new Point(37, 92);
            saveBtn.Click += new EventHandler(save_click);

            var closeBtn = new Button();
            closeBtn.Parent = this;
            closeBtn.Name = "close";
            closeBtn.Text = "关闭";
            closeBtn.Location = new Point(37, 138);
            closeBtn.Click += new EventHandler(close_click);
        }

        protected void open_Click(object sender, EventArgs e)
        {
            OpenFileDialog opnDlg = new OpenFileDialog();
            opnDlg.InitialDirectory = ".";
            opnDlg.Filter = "All | *.bmp;*.png";
            opnDlg.Title = "打开图像文件";
            opnDlg.ShowHelp = true;

            if (opnDlg.ShowDialog() == DialogResult.OK)
            {
                curFileName = opnDlg.FileName;
                try
                {
                    curBitmap = (Bitmap)Image.FromFile(curFileName);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }

            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var g = e.Graphics;
            if (curBitmap != null)
            {
                g.DrawImage(curBitmap,
                    160, 20,
                    curBitmap.Width, curBitmap.Height
                );
            }
        }
        protected void save_click(object sender, EventArgs e)
        {
            if (curBitmap != null)
            {
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.InitialDirectory = ".";
                saveDlg.Title = "保存为";
                saveDlg.OverwritePrompt = true;
                saveDlg.Filter = "BMP文件 (*.bmp) | *.bmp |"
                + "PNG文件 (*.png) | *.png";
                saveDlg.ShowHelp = true;

                if (saveDlg.ShowDialog() == DialogResult.OK)
                {
                    string fileName = saveDlg.FileName;
                    string strFilExtn = fileName.Remove(0, fileName.Length - 3);

                    switch (strFilExtn)
                    {
                        case "bmp":
                            curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        case "png":
                            curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        protected void close_click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    class HiPerfTimer
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(
            out long lpPerformanceCount
        );

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(
            out long lpFrequency
        );

        private long startTime, stopTime;
        private long freq;

        public HiPerfTimer()
        {
            startTime = 0;
            stopTime = 0;

            if (QueryPerformanceFrequency(out freq) == false)
            {
                throw new Win32Exception();
            }
        }

        public void Start()
        {
            Thread.Sleep(0);
            QueryPerformanceCounter(out startTime);
        }

        public void Stop()
        {
            QueryPerformanceCounter(out stopTime);
        }

        public double Duration
        {
            get
            {
                return (double)(stopTime - startTime) * 1000 / (double)freq;
            }
        }
    }

}